/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Model;

import DB_REDIS.Redis_Rd;
import DB_REDIS.Redis_W;
import java.util.List;
import java.util.Map;
import Object.Pet;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import libCore.Config;
import share.KeysDefinition;
import share.ShareMacros;

/**
 *
 * @author LinhTA
 */
public class ModelJet {

    String _uid;
    
    public ModelJet(String uid) {
    
        _uid = uid;
    }
 
    
    
    public Map<String,String> getJets()
    {
        Map<String,String> jets_str = new HashMap<String,String>();
        jets_str = Redis_Rd.getInstance().hget(KeysDefinition.getKeyJets(_uid));
        
        if(jets_str.isEmpty())
        {
            String typeJetDefault = Config.getParam(ShareMacros.DEFAULTUSER, "jet");
            jets_str.put(typeJetDefault, "1");
            addJet(typeJetDefault);
        }
            
        
        return jets_str;
    }
    
    public Map<String,String> buyLevel(String type)
    {
        Map<String,String> data = new HashMap<String,String>();
        data.put(ShareMacros.SUSSCES, "false");
        data.put(ShareMacros.COIN, "-1");
        data.put(ShareMacros.LEVEL, "-1");
        
        ModelItem mItem = new ModelItem();
        int coin = Integer.parseInt(mItem.getCountItem(_uid, ShareMacros.COIN));
        
        String keyJets = KeysDefinition.getKeyJets(_uid);
        String lvNow = Redis_Rd.getInstance().Hget(keyJets, type);
        int levelNow = Integer.parseInt(lvNow);
        int levelNext = levelNow + 1;
        
        int price = 0;
        try
        {
            price = Integer.parseInt(Config.getParam(ShareMacros.PRICE_LEVEL_JET,String.valueOf( levelNext)));
        }
        catch(Exception ex)
        {
            return data;
        }
         
        if(price <= coin)
        {
            long ret = Redis_W.getInstance().decrBy(KeysDefinition.getKeyCoinUser(_uid), price);
            if(ret <=0)
             return data;
            
            long lv = Redis_W.getInstance().hincrBy(keyJets, type, 1);
            data.clear();
            data.put(ShareMacros.SUSSCES, "true");
            data.put(ShareMacros.LEVEL, String.valueOf(lv));
            data.put(ShareMacros.COIN,String.valueOf(ret));
        }
        return data;
    }
    
     public Map<String,String> buyJet(String type)
    {
        Map<String,String> data = new HashMap<String,String>();
        data.put(ShareMacros.SUSSCES, "false");
        data.put(ShareMacros.COIN, "-1");
        
         ModelItem mItem = new ModelItem();
        int coin = Integer.parseInt(mItem.getCountItem(_uid, ShareMacros.COIN));
        
        String keyJets = KeysDefinition.getKeyJets(_uid);
        
        int price = 0;
        try
        {
            price = Integer.parseInt(Config.getParam(ShareMacros.PRICE_JET,type));
        }
        catch(Exception ex)
        {
            return data;
        }
         
        if(price <= coin)
        {
            long ret = Redis_W.getInstance().decrBy(KeysDefinition.getKeyCoinUser(_uid), price);
            if(ret <=0)
             return data;
            
            boolean addJet = addJet(type);
            if(addJet)
            {
                data.clear();
                data.put(ShareMacros.SUSSCES, "true");
                data.put(ShareMacros.COIN,String.valueOf(ret));
            }
            else
            {
                Redis_W.getInstance().incrBy(KeysDefinition.getKeyCoinUser(_uid), price);
            }
        }
        
        return data;
    }
     
     public boolean addJet(String type)
     {
         boolean ret = false;
         
          String keyJets = KeysDefinition.getKeyJets(_uid);
         if(Redis_Rd.getInstance().Hget(keyJets, type) != null)
             return  false;
         else
         {
             String r = Redis_W.getInstance().hsetRetries(keyJets,type, "1");
             if(r != null)
                return true;
         }
         return  ret;
     }
}
